using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using TVT.Common.Exceptions;
using System.Threading;
using System.Collections.Generic;
using TVT.Common.Helpers;
using TVT.UI.Helpers;
using TVT.BizLayer.Entities;

namespace TVT.UI.UserControls
{
    public partial class CreateUpdateAccount : System.Web.UI.UserControl
    {
        public event EventHandler OnSavedAccount = null;

        private void SavedAccount(EventArgs args)
        {            
            if (OnSavedAccount != null)
            {
                OnSavedAccount(this, args);
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlCompany.DataTextField = "Name";
                ddlCompany.DataValueField = "Id";

                ddlCompany.DataSource = Company.FindByUser((Guid)Membership.GetUser().ProviderUserKey);
                ddlCompany.DataBind();                    
                            }
        }
        
        public void EditAccount(int idAccount)
        {
            Account account = Account.Find(idAccount);
            Company company = Company.Find(account.IdCompany);
            txtAccountNumber.Text = account.Name;
            txtAccountInterest.Text = string.Format(Resources.Decimal4Format, account.Interest.ToString());
            
            ddlCompany.SelectedValue = company.Id.ToString();
            
            ViewState[WellKnownKeys.IdAccountKey] = idAccount;
            upCreateUpdateAccount.Update();
            
        }

        protected void SaveAccount(object sender, EventArgs e)
        {                              
            Account account = new Account();
            account.Name = txtAccountNumber.Text;
            account.Interest = Decimal.Parse(txtAccountInterest.Text);
            account.Company = Company.Find(Int32.Parse(ddlCompany.SelectedValue));
            account.IdCompany = account.Company.Id;
            bool newAccount = false;
            if (ViewState[WellKnownKeys.IdAccountKey] != null)
            {
                account.Id = Int32.Parse(ViewState[WellKnownKeys.IdAccountKey].ToString());
            }
            else
            {
                account.Id = 0;
            }
           
            if (account.Id == 0)
                newAccount = true;
            
            account.Save();
            
            lblTransactionResultMessage.Visible = true;
            lblTransactionResultMessage.Text = Resources.SuccesfullMessage;
            SavedAccount(new EventArgs());
            CancelAccount(this, e);
        }

        protected void CancelAccount(object sender, EventArgs e)
        {
            CleanControls();
            ViewState[WellKnownKeys.IdAccountKey] = 0;
            upCreateUpdateAccount.Update();
        }

        public void CancelAccount()
        {
            CancelAccount(this, new EventArgs());
        }

        private void CleanControls()
        {
            txtAccountNumber.Text = "";
            txtAccountInterest.Text = "";
            lblTransactionResultMessage.Visible = false;
        }
    }
}